package nl.hva.three2learn.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import nl.hva.three2learn.dao.CategoryDao;
import nl.hva.three2learn.dao.CourseDao;
import nl.hva.three2learn.dao.PostDao;
import nl.hva.three2learn.dao.TopicDao;
import nl.hva.three2learn.dao.UserDao;
import nl.hva.three2learn.model.Category;
import nl.hva.three2learn.model.Course;
import nl.hva.three2learn.model.Post;
import nl.hva.three2learn.model.Topic;
import nl.hva.three2learn.model.User;
import nl.hva.three2learn.util.Password;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	private CourseDao courseDao;
	
	@Autowired
	private CategoryDao categoryDao;

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private TopicDao topicDao;
	
	@Autowired
	private PostDao postDao;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(HttpSession session) {
		
		ModelAndView modelAndView = new ModelAndView("home");
		
		// Did an error occur while logging in?
		if (session.getAttribute("loginError") != null) {
			modelAndView.addObject("error", session.getAttribute("loginError"));
			session.removeAttribute("loginError");
		}
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/generate")
	public @ResponseBody String generateDummyModels() {
		
		Calendar calendar = Calendar.getInstance();
		
		Date courseDate = calendar.getTime();
		
		calendar.setTime(courseDate);
		calendar.add(Calendar.MONTH, 3);
		
		long coursePeriodDate = calendar.getTime().getTime();
		
		Category math = new Category("Wiskunde");
		Category computers = new Category("Computers");
		Category dutch1 = new Category("Dutch");
		Category dutch = new Category("Nederlands");
		
		List<Course> mathCourses = new LinkedList<Course>();
		mathCourses.add(new Course(
				"Plus en min leren",
				"De basis van plus en min leren beheersen.",
				courseDate,
				coursePeriodDate,
				19.99D,
				math
			));
		
		mathCourses.add(new Course(
				"Plus en min voor gevorderden",
				"Plus en min voor gevorderden.",
				courseDate,
				coursePeriodDate,
				199.99D,
				math
			));
		
		List<Course> dutchCourses = new LinkedList<Course>();
		dutchCourses.add(new Course(
				"Basis grammatica",
				"'t kofschip leren.",
				courseDate,
				coursePeriodDate,
				29.99D,
				dutch
			));
		
		List<Course> dutch1Courses = new LinkedList<Course>();
		dutch1Courses.add(new Course(
					"Basic grammar",
					"Learn the basics of dutch grammar",
					courseDate,
					coursePeriodDate,
					59.99D,
					dutch1
				));
		
		List<Course> computerCourses = new LinkedList<Course>();
		computerCourses.add(new Course(
					"Windows for dummies",
					"Learn how to use Windows with just your keyboard!",
					courseDate,
					coursePeriodDate,
					79.99D,
					computers
				));
		
		categoryDao.save(math);
		categoryDao.save(dutch);
		categoryDao.save(dutch1);
		categoryDao.save(computers);
		
		for (Course c : mathCourses)
			courseDao.save(c);
		
		for (Course c : dutchCourses)
			courseDao.save(c);
	
		for (Course c : dutch1Courses)
			courseDao.save(c);
		
		for (Course c : computerCourses)
			courseDao.save(c);
		
		math.setCourses(mathCourses);
		dutch.setCourses(dutchCourses);
		dutch1.setCourses(dutch1Courses);
		computers.setCourses(computerCourses);
		
		categoryDao.save(math);
		categoryDao.save(dutch);
		categoryDao.save(dutch1);
		categoryDao.save(computers);
		
		User user = new User();
		user.setEmail("john@three2learn.co.uk");
		user.setFirstName("John");
		user.setLastName("Doe");
		user.setHouseNumber(3);
		user.setCity("New York");
		user.setCountry("US");
		user.setPassword(Password.genHash("q1w2e3", "john@three2learn.co.uk"));
		user.setPostal("12345wc");
		user.setStreet("New york street name");
		
		userDao.save(user);
		
		user = userDao.findByEmail("john@three2learn.co.uk");
		
		for (Course c : mathCourses) {
			Topic t = new Topic(c, c.getName() + " topic title", user);
			
			topicDao.save(t);
			
			c.addTopic(t);
			
			courseDao.save(c);
			
			Post p = new Post("Hello, world!", user, t);
			
			postDao.save(p);
			
			t.addPost(p);
			
			topicDao.save(t);
		}
		
		for (Course c : dutchCourses) {
			Topic t = new Topic(c, c.getName() + " topic title", user);
			
			topicDao.save(t);
			
			c.addTopic(t);
			
			courseDao.save(c);
			
			Post p = new Post("Hello, world!", user, t);
			
			postDao.save(p);
			
			t.addPost(p);
			
			topicDao.save(t);
		}
		
		for (Course c : dutch1Courses) {
			Topic t = new Topic(c, c.getName() + " topic title", user);
			
			topicDao.save(t);
			
			c.addTopic(t);
			
			courseDao.save(c);
			
			Post p = new Post("Hello, world!", user, t);
			
			postDao.save(p);
			
			t.addPost(p);
			
			topicDao.save(t);
		}
		
		for (Course c : computerCourses) {
			Topic t = new Topic(c, c.getName() + " topic title", user);
			
			topicDao.save(t);
			
			c.addTopic(t);
			
			courseDao.save(c);
			
			Post p = new Post("Hello, world!", user, t);
			
			postDao.save(p);
			
			t.addPost(p);
			
			topicDao.save(t);
		}
		
		return "gEluKt";
	}
}